<template>
  <div>
    <el-form-item label="对齐方式">
      <el-select v-model="data.children.align"
                 placeholder="对齐方式">
        <el-option label="居左"
                   value="left"></el-option>
        <el-option label="居中"
                   value="center"></el-option>
        <el-option label="居右"
                   value="right"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="头部对齐方式">
      <el-select v-model="data.children.headerAlign"
                 placeholder="对齐方式">
        <el-option label="居左"
                   value="left"></el-option>
        <el-option label="居中"
                   value="center"></el-option>
        <el-option label="居右"
                   value="right"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="表单格式">
      <el-select v-model="data.children.type"
                 placeholder="表单格式">
        <el-option label="表格"
                   value=""></el-option>
        <el-option label="表单"
                   value="form"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="序号"
                  v-if="data.children.type === 'form'">
      <el-switch v-model="data.children.index"></el-switch>
    </el-form-item>
    <el-form-item label="添加按钮">
      <el-switch v-model="data.children.addBtn"></el-switch>
    </el-form-item>
    <el-form-item label="删除按钮">
      <el-switch v-model="data.children.delBtn"></el-switch>
    </el-form-item>
    <el-form-item label="是否只读">
      <el-switch v-model="data.readonly"></el-switch>
    </el-form-item>
    <el-form-item label="是否禁用">
      <el-switch v-model="data.disabled"></el-switch>
    </el-form-item>
    <el-form-item label="是否可见">
      <el-switch v-model="data.display"></el-switch>
    </el-form-item>
    <el-form-item label="列合计">
      <el-switch v-model="data.children.showSummary"></el-switch>
    </el-form-item>
    <el-form-item label="是否可搜索子表内容">
      <el-switch v-model="data.children.searchShow"></el-switch>
    </el-form-item>
    <div v-if="data.children.showSummary">
      <el-button type="text" @click="dialogVisible=true">添加统计列</el-button>
      <el-table
          :data="sumColumnList"
          style="width: 100%">
        <el-table-column prop="name" label="列名称">
          <template #default="{row}">
            {{ dynamicColumnMap[row.name] ? dynamicColumnMap[row.name].label : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="统计方式">
          <template #default="{row}">
            {{ summaryDicMap[row.type] }}
          </template>

        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="text" size="small" @click="delSumColumn(scope)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

    </div>
    <el-dialog title="编辑"
               v-model="dialogVisible"
               width="400"
               append-to-body>
      <el-form ref="form" :model="editSummaryModel" size="small" label-width="80px">
        <el-form-item label="统计列">
          <el-select style="width: 100%" v-model="editSummaryModel.name" placeholder="请选择列信息">
            <el-option v-for="(item,index) in candidateColumn" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="统计方式">
          <el-select style="width: 100%" v-model="editSummaryModel.type" placeholder="请选择类型">
            <el-option v-for="(item,index) in summaryDicMap" :label="item" :value="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="精度">
          <el-input-number style="width: 100%" controls-position="right" v-model="editSummaryModel.decimals"
                           placeholder="精度" :min="0" :max="8"></el-input-number>
        </el-form-item>
        <el-form-item label="标签名称">
          <el-input v-model="editSummaryModel.label"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancelEditDialog">取消</el-button>
          <el-button type="primary" @click="submitSummaryModel">保存</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {deepClone} from "utils/util";

export default {
  name: "config-dynamic",
  props: ['data'],
  computed: {
    summaryDicMap() {
      return {"sum": '累加', 'avg': '平均值', 'count': '数量统计', '': '标签'}
    },
    dynamicColumnMap() {
      let srcColumns = this.data.children.column || [];
      let sumColumnMap = {};
      for (let i = 0; i < srcColumns.length; i++) {
        sumColumnMap[srcColumns[i].prop] = srcColumns[i]
      }
      return sumColumnMap;
    },
    candidateColumn() {
      let srcColumns = this.data.children.column || [];
      let sumColumnMap = {};
      for (let i = 0; i < this.sumColumnList.length; i++) {
        sumColumnMap[this.sumColumnList[i].name] = this.sumColumnList[i]
      }
      let result = srcColumns.map(e => {
        return {
          label: e.label,
          value: e.prop,
          disabled: sumColumnMap[e.prop]
        }
      })

      return result;
    },

    sumColumnList() {
      let sumColumnList = this.data.children.sumColumnList || []
      return sumColumnList;
    }
  },
  data() {
    return {
      dialogVisible: false,
      editSummaryModel: {}
    }
  },
  created() {
    let searchShow = this.data.children.searchShow
    if (searchShow === null || searchShow === undefined) {
      this.data.children.searchShow = false
    }
  },
  methods: {
    cancelEditDialog() {
      this.editSummaryModel = {};
      this.dialogVisible = false
    },
    delSumColumn(data) {
      let index = data.$index;
      this.data.children.sumColumnList.splice(index, 1)
    },
    submitSummaryModel() {
      if (!this.editSummaryModel.name) {
        this.$message.error("请选择需要统计的列")
        return
      }
      let data = deepClone(this.editSummaryModel)
      if (!this.data.children.sumColumnList) {
        this.data.children.sumColumnList = []
      }
      this.data.children.sumColumnList.push(data)
      console.log("this.data.children.sumColumnList", this.data.children.sumColumnList)
      this.editSummaryModel = {}
      this.dialogVisible = false

    }
  }
}
</script>
